package pl.prv.polanie.client.model;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dto.CSAcademicTitleDTO;
import pl.prv.polanie.client.dto.CSBuildingDTO;
import pl.prv.polanie.client.dto.CSCalendarDayDTO;
import pl.prv.polanie.client.dto.CSDeclarationDTO;
import pl.prv.polanie.client.dto.CSGroupDTO;
import pl.prv.polanie.client.dto.CSLecturerDTO;
import pl.prv.polanie.client.dto.CSPreferenceDTO;
import pl.prv.polanie.client.dto.CSPreferenceTypeDTO;
import pl.prv.polanie.client.dto.CSRoomDTO;
import pl.prv.polanie.client.dto.CSRoomTypeDTO;
import pl.prv.polanie.client.dto.CSSemesterDTO;
import pl.prv.polanie.client.dto.CSSemesterTypeDTO;
import pl.prv.polanie.client.dto.CSSemestersYearGroupsDTO;
import pl.prv.polanie.client.dto.CSStudiesTypeDTO;
import pl.prv.polanie.client.dto.CSSubgroupDTO;
import pl.prv.polanie.client.dto.CSSubgroupTypeDTO;
import pl.prv.polanie.client.dto.CSSubjectDTO;
import pl.prv.polanie.client.dto.CSSubjectEventDTO;
import pl.prv.polanie.client.dto.CSSubjectNameDTO;
import pl.prv.polanie.client.dto.CSSubjectTypeDTO;
import pl.prv.polanie.client.dto.CSVAcademicTitleDTO;
import pl.prv.polanie.client.dto.CSVBuildingDTO;
import pl.prv.polanie.client.dto.CSVCalendarDayDTO;
import pl.prv.polanie.client.dto.CSVDeclarationDTO;
import pl.prv.polanie.client.dto.CSVGroupDTO;
import pl.prv.polanie.client.dto.CSVLecturerDTO;
import pl.prv.polanie.client.dto.CSVPreferenceDTO;
import pl.prv.polanie.client.dto.CSVPreferenceTypeDTO;
import pl.prv.polanie.client.dto.CSVRoomDTO;
import pl.prv.polanie.client.dto.CSVRoomTypeDTO;
import pl.prv.polanie.client.dto.CSVSemesterDTO;
import pl.prv.polanie.client.dto.CSVSemesterTypeDTO;
import pl.prv.polanie.client.dto.CSVSemestersYearGroupsDTO;
import pl.prv.polanie.client.dto.CSVStudiesTypeDTO;
import pl.prv.polanie.client.dto.CSVSubgroupDTO;
import pl.prv.polanie.client.dto.CSVSubgroupTypeDTO;
import pl.prv.polanie.client.dto.CSVSubjectDTO;
import pl.prv.polanie.client.dto.CSVSubjectEventDTO;
import pl.prv.polanie.client.dto.CSVSubjectNameDTO;
import pl.prv.polanie.client.dto.CSVSubjectTypeDTO;
import pl.prv.polanie.client.dto.CSVYearGroupNameDTO;
import pl.prv.polanie.client.dto.CSYearGroupNameDTO;

/**
 * @author Janusz Prokulewicz
 * Created: May 16, 2008
 * 
 * Klasa odpowiedzialna za tworzenie (skladanie) obiektow dto uzywanych podczas
 * generowania kontrolek dla warstwy widoku. 
 */
public class ViewDataProvider {

	public static CSVPreferenceTypeDTO getCSVPreferenceType(CSPreferenceTypeDTO dto) {
		CSVPreferenceTypeDTO viewDTO = new CSVPreferenceTypeDTO();
		
		viewDTO.setId(dto.getId());
		viewDTO.setName(dto.getName());
		viewDTO.setColor(dto.getColor());
		
		//
		return viewDTO;

	}
	
	public static CSVRoomTypeDTO getCSVRoomType(CSRoomTypeDTO dto) {
		CSVRoomTypeDTO viewDTO = new CSVRoomTypeDTO();
		
		viewDTO.setId(dto.getId());
		viewDTO.setName(dto.getName());
		return viewDTO;
	}
	
	public static CSVBuildingDTO getCSVBuilding(CSBuildingDTO dto) {
		CSVBuildingDTO viewDTO = new CSVBuildingDTO();
		
		viewDTO.setId(dto.getId());
		viewDTO.setName(dto.getName());
		viewDTO.setSymbol(dto.getSymbol());
		return viewDTO;
	}
	
	public static CSVAcademicTitleDTO getCSVAcademicTitle(CSAcademicTitleDTO dto) {
		CSVAcademicTitleDTO viewDTO = new CSVAcademicTitleDTO();
		
		viewDTO.setId(dto.getId());
		viewDTO.setTitle(dto.getTitle());
		
		return viewDTO;
	}
	
	public static CSVSubjectNameDTO getCSVSubjectName(CSSubjectNameDTO dto) {
		CSVSubjectNameDTO viewDTO = new CSVSubjectNameDTO();
		
		viewDTO.setId(dto.getId());
		viewDTO.setName(dto.getName());
		viewDTO.setSymbol(dto.getSymbol());
		
		return viewDTO;
	}
	
	public static CSVSubjectTypeDTO getCSVSubjectType(CSSubjectTypeDTO dto) {
		CSVSubjectTypeDTO viewDTO = new CSVSubjectTypeDTO();
	
		viewDTO.setId(dto.getId());
		viewDTO.setName(dto.getName());
		viewDTO.setInitial(dto.getInitial());
		
		return viewDTO;
	}
	
	public static CSVSubgroupTypeDTO getCSVSubgroupType(CSSubgroupTypeDTO dto) {
		CSVSubgroupTypeDTO viewDTO = new CSVSubgroupTypeDTO();

		viewDTO.setId(dto.getId());
		viewDTO.setName(dto.getName());
		
		return viewDTO;
	}

	public static CSVSemesterTypeDTO getCSVSemesterType(CSSemesterTypeDTO dto) {
		CSVSemesterTypeDTO viewDTO = new CSVSemesterTypeDTO();

		viewDTO.setId(dto.getId());
		viewDTO.setName(dto.getName());
		
		return viewDTO;
	}
	
	public static CSVStudiesTypeDTO getCSVStudiesType(CSStudiesTypeDTO dto) {
		CSVStudiesTypeDTO viewDTO = new CSVStudiesTypeDTO();
		
		viewDTO.setId(dto.getId());
		viewDTO.setName(dto.getName());
		
		return viewDTO;
	}
	
	public static CSVPreferenceDTO getCSVPreference(CSPreferenceDTO dto) {
		CSVPreferenceDTO viewDTO = new CSVPreferenceDTO();
		
		viewDTO.setId(dto.getId());
		
		//preferenceType
		long ptID = dto.getPreferenceTypeID();
		CSPreferenceTypeDTO csPT = Cache.getCSPreferenceTypeDTOByID(ptID);
		CSVPreferenceTypeDTO csvPT = ViewDataProvider.getCSVPreferenceType(csPT);

		viewDTO.setPreferenceTypeColor(csvPT.getColor());
		viewDTO.setPreferenceTypeName(csvPT.getName());

		//lecturer
		long lID = dto.getLecturerID();
		if (lID != -1) {
			CSLecturerDTO csL = Cache.getCSLecturerDTOByID(lID);
			CSVLecturerDTO csvL = ViewDataProvider.getCSVLecturer(csL);
			
			viewDTO.setLecturer(csvL.toLabelShort());
		} else {
			viewDTO.setLecturer("-no lecturer-");
		}
		
		viewDTO.setDuration(dto.getDuration());
		viewDTO.setStartTime(dto.getStartTime());
		
		return viewDTO;
	}
	
	public static CSVSemesterDTO getCSVSemester(CSSemesterDTO dto) {
		CSVSemesterDTO viewDTO = new CSVSemesterDTO();
		
		viewDTO.setId(dto.getId());
		
		//semesterType
		long setID = dto.getSemesterTypeID();
		CSSemesterTypeDTO csSET = Cache.getCSSemesterTypeDTOByID(setID);
		CSVSemesterTypeDTO csvSET = ViewDataProvider.getCSVSemesterType(csSET);
		viewDTO.setSemesterType(csvSET.toLabel());
		
		
		//studiesType
		long sttID = dto.getStudiesTypeID();
		CSStudiesTypeDTO csSTT = Cache.getCSStudiesTypeDTOByID(sttID);
		CSVStudiesTypeDTO csvSTT = ViewDataProvider.getCSVStudiesType(csSTT);
		viewDTO.setStudiesType(csvSTT.toLabel());
		
		//yearNumber
		viewDTO.setYearNumber("" + dto.getYearNumber());
		viewDTO.setVisibleForOrdinaryUsers(dto.isVisibleForOrdinaryUsers());

		return viewDTO;
	}

	public static CSVLecturerDTO getCSVLecturer(CSLecturerDTO dto) {
		CSVLecturerDTO viewDTO = new CSVLecturerDTO();
		
		viewDTO.setId(dto.getId());
		
		//academicTitle
		long atID = dto.getAcademicTitleID();
		CSAcademicTitleDTO csAT = Cache.getCSAcademicTitleDTOByID(atID);
		CSVAcademicTitleDTO csvAT = ViewDataProvider.getCSVAcademicTitle(csAT);
		
		viewDTO.setAcademicTitle(csvAT.toLabel());
		
		viewDTO.setName(dto.getName());
		viewDTO.setSurname(dto.getSurname());

		return viewDTO;
	}
	

	public static CSVYearGroupNameDTO getCSVYearGroupName(CSYearGroupNameDTO dto) {
		CSVYearGroupNameDTO viewDTO = new CSVYearGroupNameDTO();
		
		viewDTO.setId(dto.getId());		
		viewDTO.setName(dto.getName());
		
		return viewDTO;
	}
	

	public static CSVSemestersYearGroupsDTO getCSVSemestersYearGroups(CSSemestersYearGroupsDTO dto) {
		CSVSemestersYearGroupsDTO viewDTO = new CSVSemestersYearGroupsDTO();

		viewDTO.setId(dto.getId());
		
		//yearGroupName
		long ygnID = dto.getYearGroupNameID();
		CSYearGroupNameDTO csYGN = Cache.getCSYearGroupNameDTOByID(ygnID);
		CSVYearGroupNameDTO csvYGN = ViewDataProvider.getCSVYearGroupName(csYGN);
		
		viewDTO.setYearGroupName(csvYGN.toLabel());
		
		//semester
		long sID = dto.getSemesterID();
		CSSemesterDTO csS = Cache.getCSSemesterDTOByID(sID);
		CSVSemesterDTO csvS = ViewDataProvider.getCSVSemester(csS);

		viewDTO.setSemester(csvS.toLabel());
		
		
		//
		viewDTO.setStudentsNumber(dto.getStudentsNumber());
		
		return viewDTO;
	}

	
	public static CSVGroupDTO getCSVGroup(CSGroupDTO dto) {
		CSVGroupDTO viewDTO = new CSVGroupDTO();
		
		viewDTO.setId(dto.getId());
		
		viewDTO.setName(dto.getName());
		viewDTO.setStudentsNumber(dto.getStudentsNumber());
		
		
		//semestersYearGroup
		long sygID = dto.getSemestersYearGroupID();
		CSSemestersYearGroupsDTO csSYG = Cache.getCSSemestersYearGroupsDTOByID(sygID);
		if (csSYG != null) {
			CSVSemestersYearGroupsDTO csvSYG = ViewDataProvider.getCSVSemestersYearGroups(csSYG);
			viewDTO.setSemestersYearGroup(csvSYG.toLabel());
		} else {
			viewDTO.setSemestersYearGroup("(" + sygID + ") syg not found in cache");
		}

		return viewDTO;
	}
	
	public static CSVSubgroupDTO getCSVSubgroup(CSSubgroupDTO dto) {
		CSVSubgroupDTO viewDTO = new CSVSubgroupDTO();

		viewDTO.setId(dto.getId());
		
		//group
		long gID = dto.getGroupID();
//		Planner.log("ViewDataProvider.getCSVSubgroup: " + "chce pobrac group oo ID -> " + gID);
		CSGroupDTO csG = Cache.getCSGroupDTOByID(gID);
		if (csG != null) {
			CSVGroupDTO csvG = ViewDataProvider.getCSVGroup(csG);
			viewDTO.setGroup(csvG.toLabel());
		} else {
			viewDTO.setGroup("(" + gID + ") group not found in cache");
		}
		
		
		
		//subgroupType
		long stID = dto.getSubgroupTypeID();
		CSSubgroupTypeDTO csST = Cache.getCSSubgroupTypeDTOByID(stID);
		if (csST != null) {
			CSVSubgroupTypeDTO csvST = ViewDataProvider.getCSVSubgroupType(csST);
			viewDTO.setSubgroupType(csvST.toLabel());
		} else {
			viewDTO.setSubgroupType("(" + stID + ") subgroup type not found in cache");
		}

		

		//
		viewDTO.setName(dto.getName());
		viewDTO.setStudentsNumber(dto.getStudentsNumber());

		return viewDTO;
	}
	
	public static CSVRoomDTO getCSVRoom(CSRoomDTO dto) {
		CSVRoomDTO viewDTO = new CSVRoomDTO();
		
		//
		viewDTO.setId(dto.getId());
		viewDTO.setName(dto.getName());
		viewDTO.setCapacity(dto.getCapacity());
		
		
		//roomType
		long rtID = dto.getRoomTypeID();
		CSRoomTypeDTO csRT = Cache.getCSRoomTypeDTOByID(rtID);
		CSVRoomTypeDTO csvRT = ViewDataProvider.getCSVRoomType(csRT);
		
		viewDTO.setRoomType(csvRT.toLabel());
		
		//building
		long bID = dto.getBuildingID();
		CSBuildingDTO csB = Cache.getCSBuildingDTOByID(bID);
		CSVBuildingDTO csvB = ViewDataProvider.getCSVBuilding(csB);
		
		viewDTO.setBuilding(csvB.toLabel());

		
		
		return viewDTO;
	}

	public static CSVSubjectDTO getCSVSubject(CSSubjectDTO dto) {
		CSVSubjectDTO viewDTO = new CSVSubjectDTO();
		
		viewDTO.setId(dto.getId());
		
		//subjectName
		long snID = dto.getSubjectNameID();
		CSSubjectNameDTO csSN = Cache.getCSSubjectNameDTOByID(snID);
		CSVSubjectNameDTO csvSN = ViewDataProvider.getCSVSubjectName(csSN);
		
		viewDTO.setSubjectNameName(csvSN.getName());
		viewDTO.setSubjectNameSymbol(csvSN.getSymbol());
		
		
		
		//subjectType
		long stID = dto.getSubjectTypeID();
		CSSubjectTypeDTO csST = Cache.getCSSubjectTypeDTOByID(stID);
		CSVSubjectTypeDTO csvST = ViewDataProvider.getCSVSubjectType(csST);
		
		viewDTO.setSubjectTypeName(csvST.getName());
		viewDTO.setSubjectTypeInitial(csvST.getInitial());
		
		return viewDTO;
	}

	
	public static CSVSubjectEventDTO getCSVSubjectEvent(CSSubjectEventDTO dto) {
		CSVSubjectEventDTO viewDTO = new CSVSubjectEventDTO();
		
		//TODO
		viewDTO.setId(dto.getId());
//		viewDTO.setCalendarDay("csvCalenndarDay");
		viewDTO.setDuration(dto.getDuration());
		viewDTO.setEventSort(dto.getEventSort());
		
		
		//calendarDay
		long cdID = dto.getCalendarDayID();
		if (cdID != -1) {
			CSCalendarDayDTO csCD = Cache.getCSCalendarDayDTOByID(cdID);
			CSVCalendarDayDTO csvCD = ViewDataProvider.getCSVCalendarDay(csCD);
			
			viewDTO.setCalendarDay(csvCD.toLabel());
		} else {
			viewDTO.setCalendarDay("-not scheduled-");
		}
		
		
		//lecturer
		long lID = dto.getLecturerID();
		if (lID != -1) {
			CSLecturerDTO csL = Cache.getCSLecturerDTOByID(lID);
			CSVLecturerDTO csvL = ViewDataProvider.getCSVLecturer(csL);
			
			viewDTO.setLecturer(csvL.toLabel());

			
			viewDTO.setLecturerName(csvL.getName());
			viewDTO.setLecturerSurname(csvL.getSurname());
			viewDTO.setLecturerAcademicTitle(csvL.getAcademicTitle());
			
		} else {
			viewDTO.setLecturer("-no lecturer-");
			viewDTO.setLecturerSurname("-no lecturer-");
		}
		
		
		//subject
		long sID = dto.getSubjectID();
		if (sID != -1) {
			CSSubjectDTO csS = Cache.getCSSubjectDTOByID(sID);
			CSVSubjectDTO csvS = ViewDataProvider.getCSVSubject(csS);
			viewDTO.setSubject(csvS.toLabel());
			
			viewDTO.setSubjectNameName(csvS.getSubjectNameName());
			viewDTO.setSubjectNameSymbol(csvS.getSubjectNameSymbol());
			viewDTO.setSubjectTypeName(csvS.getSubjectTypeName());
			viewDTO.setSubjectTypeInitial(csvS.getSubjectTypeInitial());
			
		} else {
			viewDTO.setSubject("-no subject-");
		}

		
		//room
		long rID = dto.getRoomID();
		if (rID != -1) {
			CSRoomDTO csR = Cache.getCSRoomDTOByID(rID);
			CSVRoomDTO csvR = ViewDataProvider.getCSVRoom(csR);
			viewDTO.setRoom(csvR.toLabel());
		} else {
			viewDTO.setRoom("-no room-");
		}
		
		//subgroup
		long subgID = dto.getSubgroupID();
		if (subgID != -1) {
			CSSubgroupDTO csSubg = Cache.getCSSubgroupDTOByID(subgID);
			if (csSubg != null) {
				CSVSubgroupDTO csvSubg = ViewDataProvider.getCSVSubgroup(csSubg);
				viewDTO.setSubgroup(csvSubg.toLabelShort());
			} else {
				Planner.log("ViewDataProvider.getCSVSubjectEvent: " + "NIE MA W CACHE SUBGROUP O ID " + subgID);
				viewDTO.setSubgroup("(" + subgID + ") subgroup not found in cache");
			}
		} else {
			viewDTO.setSubgroup("-no subgroup-");
		}		
		
		
		return viewDTO;
	}
	
	public static CSVDeclarationDTO getCSVDeclaration(CSDeclarationDTO dto) {
		CSVDeclarationDTO viewDTO = new CSVDeclarationDTO();
		
		//TODO
		viewDTO.setId(dto.getId());
//		viewDTO.setCalendarDay("csvCalenndarDay");
		viewDTO.setDuration(dto.getDuration() / 45);
//		viewDTO.setSubgroupsAmount(dto.getSubgroupsAmount());
		
		if (dto.getToSchedule() != null) {
			viewDTO.setAlreadyScheduled(dto.getToSchedule());
		} else {
			viewDTO.setAlreadyScheduled(new Integer(0));
		}
		
		
		//group
		long gID = dto.getGroupID();
		if (gID != -1) {
			CSGroupDTO csG = Cache.getCSGroupDTOByID(gID);
			CSVGroupDTO csvG = ViewDataProvider.getCSVGroup(csG);
			
			viewDTO.setGroup(csvG.toLabel());
		} else {
			viewDTO.setGroup("-no group-");
		}
		
		
		//subgroup type
		long stID = dto.getSubgroupTypeID();
		if (stID != -1) {
			CSSubgroupTypeDTO csST = Cache.getCSSubgroupTypeDTOByID(stID);
			CSVSubgroupTypeDTO csvST = ViewDataProvider.getCSVSubgroupType(csST);
			
			viewDTO.setSubgroupType(csvST.toLabel());
		} else {
			viewDTO.setSubgroupType("-no subgroup type-");
		}
		
		
		//subject
		long sID = dto.getSubjectID();
		if (sID != -1) {
			CSSubjectDTO csS = Cache.getCSSubjectDTOByID(sID);
			CSVSubjectDTO csvS = ViewDataProvider.getCSVSubject(csS);
			viewDTO.setSubject(csvS.toLabel());
		} else {
			viewDTO.setSubject("-no subject-");
		}
		


		return viewDTO;
	}

	public static CSVCalendarDayDTO getCSVCalendarDay(CSCalendarDayDTO dto) {
		CSVCalendarDayDTO viewDTO = new CSVCalendarDayDTO();
		
		viewDTO.setId(dto.getId());
		viewDTO.setDayLabel(dto.getDayLabel());
		viewDTO.setDisplayOrder(dto.getDisplayOrder());
		
		String sunday = "saturday";
		if (dto.isSunday()) {
			sunday = "sunday";
		}
		viewDTO.setSunday(sunday);
		
		
		//semester
		long sID = dto.getSemesterID();
		if (sID != -1) {
			CSSemesterDTO csS = Cache.getCSSemesterDTOByID(sID);
			CSVSemesterDTO csvS = ViewDataProvider.getCSVSemester(csS);
			
			viewDTO.setSemester(csvS.toLabel());
		} else {
			viewDTO.setSemester("-no semester-");
		}
		
		return viewDTO;
	}
	
	
}
